package com.sinosoft.sinopay.apiservise.chinapay.dto;

import com.sinosoft.sinocloud.platform.base.SinoBaseDTO;
import com.sinosoft.sinopay.apiservise.chinapay.util.ChinapayConfig;
import com.sinosoft.sinopay.apiservise.chinapay.util.ChinapaySignUtil;
import com.sinosoft.sinopay.apiservise.chinapay.util.PaymentUtil;

/**
 * 【上银联请求参数封装bean】
 * @author chihouliang
 * @date 2016年9月13日 下午2:58:36 
 * @version V1.0
 */
@SuppressWarnings("serial")
public class ChinaPayParaDto extends SinoBaseDTO {
    
    /**商户号*/
    private String merId;
    /**商户日期*/
    private String transDate;
    /**订单号*/
    private String orderNo;
    /**交易类型*/
    private String transType;
    /**开户行号*/
    private String openBankId;
    /**卡折标志*/
    private String cardType;
    /**卡号/折号*/
    private String cardNo;
    /**持卡人姓名*/
    private String usrName;
    /**证件类型*/
    private String certType;
    /**证件号*/
    private String certId;
    /**币种*/
    private String curyId;
    /**金额*/
    private String transAmt;
    /**用途*/
    private String purpose;
    /**私有域*/
    private String priv1;
    /**版本号*/
    private String version;
    /**网关号*/
    private String gateId;
    /**签名值*/
    private String chkValue;

    public ChinaPayParaDto(String transDate, String orderNo, String openBankId, String cardType, String cardNo,
            String usrName, String certType, String certId, String transAmt, String purpose, String priv1) {
        this.merId = ChinapayConfig.getMerId();
        this.transDate = transDate;
        this.orderNo = orderNo;
        this.transType =ChinapayConfig.getTransType();
        this.openBankId = openBankId;
        this.cardType = cardType;
        this.cardNo = cardNo;
        //持卡人姓名：中文转为uncode编码
        this.usrName = PaymentUtil.toUnicodeString(usrName);
        this.certType = certType;
        this.certId = certId;
        this.curyId = ChinapayConfig.getCuryId();
        this.transAmt = transAmt;
        this.purpose = purpose;
        //私有域：中文转为uncode编码
        this.priv1 = PaymentUtil.toUnicodeString(priv1);
        this.version = ChinapayConfig.getVersion();
        this.gateId = ChinapayConfig.getGateId();
        
        //上银联数字签名
        String signValues = getToSignValues();
        this.chkValue = ChinapaySignUtil.sign(signValues);
    }

    /**
     * 
     * 获取需要签名的字符集合
     * @return
     */
    public String getToSignValues() {
        StringBuffer sb =  new StringBuffer();
        sb.append(merId);
        sb.append(transDate);
        sb.append(orderNo);
        sb.append(transType);
        sb.append(openBankId);
        sb.append(cardType);
        sb.append(cardNo);
        sb.append(usrName);
        sb.append(certType);
        sb.append(certId);
        sb.append(curyId);
        sb.append(transAmt);
        sb.append(priv1);
        sb.append(version);
        sb.append(gateId);
        return sb.toString();
    }

    /**
     * 
     * 获取请求参数拼接字符串，如：merId=merId&transDate=transDate
     * @return 请求参数拼接字符串，包括签名。创建对象时已经签名处理，不需额外签名操作。
     */
    public String getRequestPara() {
        
        StringBuffer sb = new StringBuffer();
        sb.append("merId=").append(merId).append("&");
        sb.append("transDate=").append(transDate).append("&");
        sb.append("orderNo=").append(orderNo).append("&");
        sb.append("transType=").append(transType).append("&");
        sb.append("openBankId=").append(openBankId).append("&");
        sb.append("cardType=").append(cardType).append("&");
        sb.append("cardNo=").append(cardNo).append("&");
        sb.append("usrName=").append(usrName).append("&");
        sb.append("certType=").append(certType).append("&");
        sb.append("certId=").append(certId).append("&");
        sb.append("curyId=").append(curyId).append("&");
        sb.append("transAmt=").append(transAmt).append("&");
        sb.append("purpose=").append(purpose).append("&");
        sb.append("priv1=").append(priv1).append("&");
        sb.append("version=").append(version).append("&");
        sb.append("gateId=").append(gateId).append("&");
        sb.append("chkValue=").append(chkValue).append("&");

        return sb.toString();
    }
 
    /**
     * 返回数字签名值，签名在创建对象时生成。
     */
    public String getChkValue() {
        return this.chkValue;
    } 

}
